$(function () {
    // 编辑部门
    $('#md_edit_department').on('show.bs.modal', function (event) {
        var _e = $(event.relatedTarget);
        var id = _e.data("id");
        var parentId = _e.data("parent-id");
        var name = _e.data("name");

        var form = window.form_edit_department;

        $(form.id).val(id);
        $(form.parentId).val(parentId);
        $(form.name).val(name);
    });
    $("[name='btn_save_edit_department']").click(function () {
        var form = window.form_edit_department;
        var id = form.id.value;
        var name = form.name.value;
        var parentId = form.parentId.value;

        var department = {id: id, name: name, parentId: parentId};
        $.putJsonBody("/department/" + id, department, function (re) {
        	window.location.href=window.location.href; 
        	window.location.reload; 
        })
    });

    // 添加子部门
    $('#md_new_department').on('show.bs.modal', function (event) {
        var _e = $(event.relatedTarget);
        var parentId = _e.data("id");
        var form = window.form_new_department;
        $(form.parentId).val(parentId);
    });
    $("[name='btn_save_new_department']").click(function () {
        var form = window.form_new_department;
        var parentId = form.parentId.value;
        var name = form.name.value;

        var department = {name: name, parentId: parentId};
        $.postJsonBody("/department", department, function (re) {
        	window.location.href=window.location.href; 
        	window.location.reload; 
        })
    });


    //删除部门
    $("[name='btn_delete_department']").click(function () {
        var departmentId = $(this).data("id");
        bootbox.bconfirm("确定要删除这个部门吗？", function (re) {
            if (re) {
                $.delete("/department/" + departmentId, function (re) {
                    if (re.data) {
                    	window.location.href=window.location.href; 
                    	window.location.reload; 
                    } else {
                        tipper.danger("删除失败，请重试");
                    }
                });
            }
        });
    });


    //加载异步数据
    $.get("/department/list", function (re) {
        var companyTree = re.data;
        var form = window.form_new_product;
        var opts_html = "";
        opts_html = departmentOptionMaker(companyTree, 0);

        $(window.form_new_department.parentId).append(opts_html);
        $(window.form_edit_department.parentId).append(opts_html);
    });
});


function departmentOptionMaker(companyList, level) {
    var opts_html = "";
    for (var i in companyList) {
        var company = companyList[i];

        var prefix = "";
        for (i = 0; i < level; i++) {
            prefix += "&nbsp;&nbsp;&nbsp;&nbsp;";
        }
        prefix += prefix.length == 0 ? "" : "|-";
        opts_html += "<option value='" + company.id + "'>" + prefix + company.name + "</option>";
        if (company.children.length > 0) {
            opts_html += departmentOptionMaker(company.children, level + 1);
        }
    }
    return opts_html;
}